package com.redoor.app.stake.basicdata.mq.consumerService;

import com.aliyun.openservices.ons.api.Consumer;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.redoor.app.stake.basicdata.model.constants.BasePropConstants;
import com.redoor.app.stake.basicdata.service.BasePropService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import java.util.Properties;

/**
 * Created by DELL on 2018/4/10.
 */
public class ChargePoleVersionMQListenerService {
    private static final Logger logger = LoggerFactory.getLogger(ChargePoleVersionMQListenerService.class);

    @Autowired
    private BasePropService basePropService;

    private Consumer consumer ;

    @PostConstruct
    public void  init(){
        try {
            logger.info("init ChargePoleVersionMQListenerService ...");
            Properties cprop = new Properties();
            cprop.put(PropertyKeyConst.AccessKey, this.basePropService.getProp(BasePropConstants.consumerAccessKey));
            cprop.put(PropertyKeyConst.SecretKey, this.basePropService.getProp(BasePropConstants.consumerSecretKey));
            cprop.put(PropertyKeyConst.ONSAddr, this.basePropService.getProp(BasePropConstants.namesrvAddr));
            cprop.put(PropertyKeyConst.ConsumerId, this.basePropService.getProp(BasePropConstants.customerId1));
            if(this.basePropService.initMQFlag()){
                consumer = ONSFactory.createConsumer(cprop);
                consumer.subscribe("charger_mangement_mqup", "PcModelStakeRel", new ChargePoleVersionMessageListener());
                consumer.start();
                logger.info("init ChargePoleVersionMQListenerService successfully get mq consumer = " + consumer);
            } else {
                logger.warn("not init ChargePoleVersionMQListenerService because flag is flase");
            }
        } catch (Exception e){
            logger.error("init ChargePoleVersionMQListenerService failure",e);
        }
    }
}
